<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Agent;

use App\Constants\Constants;
use App\Dao\BusinessDao;
use App\Dao\AgentMoneyDao;
use App\Dao\AgentDao;
use App\Exception\CustomException;
use App\Job\IntegralJob;
use App\Kernel\Util\UserJwtInstance;

use App\Service\Service;

use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

/**
 * 金额服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class MoneyService extends Service
{
    /**
     * @Inject
     * @var AgentMoneyDao
     */
    public $agent_money_dao;

    /**
     * @Inject
     * @var AgentDao
     */
    public $agent_dao;

    /**
     * @Inject
     * @var BusinessDao
     */
    public $business_dao;

    /**
     * 代理商金额更新
     * @param int $user_id
     * @return mixed
     */
    public function updateMoneyAgentBalance(int $user_id)
    {
        $balance_income = $this->sumMoneyBalance( [
            'agent_id' => $user_id,
            'type' => 1
        ] );

        $balance_expenditure = $this->sumMoneyBalance( [
            'agent_id' => $user_id,
            'type' => 2
        ] );

        $commission = $this->sumMoneyBalance( [ // 渠道商
            'agent_id' => $user_id,
            'price_source' => 2,
            'source' => 4,
            'type' => 1
        ] );

        $store = $this->sumMoneyBalance( [ //商家
            'agent_id' => $user_id,
            'price_source' => 1,
            'type' => 1
        ] );

        $balance = filterMoney($balance_income - $balance_expenditure, 4);

        $this->agent_dao->updateMap($user_id,
            [
                'money' => $balance
            ]
        );

        $this->redis->hSet('txh:agent:'.$user_id, 'money', $balance); //总金额

        $this->redis->hSet( 'txh:agent:commission:'.$user_id, 'money', $commission);

        $this->redis->hSet( 'txh:agent:store:'.$user_id, 'money', $store);


        return [];
    }

    /**
     * 获取代理商实时佣金缓存 - 商家
     * @param int $user_id
     * @return mixed
     */
    public function getMoneyBalanceByStore(int $user_id)
    {
        $money =  $this->redis->hGet( 'txh:agent:store:'.$user_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 获取代理商实时佣金缓存 - 渠道商
     * @param int $user_id
     * @return mixed
     */
    public function getMoneyBalanceByCommission(int $user_id)
    {
        $money =  $this->redis->hGet( 'txh:agent:commission:'.$user_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 获取代理实时缓存金额 --- 总金额
     * @param int $user_id
     * @return string
     */
    public function getMoneyBalanceByAgent(int $user_id)
    {
        $money =  $this->redis->hGet( 'txh:agent:'.$user_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 实时计算店铺积分 --- 按条件
     * @param array $map
     * @return mixed
     */
    public function sumMoneyBalance(array $map)
    {
        return $this->agent_money_dao->sumMoneyBalance( $map );
    }
}